package cn.learn.service.cargo.impl;

import cn.learn.dao.cargo.ContractDao;
import cn.learn.dao.cargo.ContractProductDao;
import cn.learn.dao.cargo.ExtCproductDao;
import cn.learn.domain.cargo.*;
import cn.learn.service.cargo.ContractProductService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ContractDao contractDao;
    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ContractProduct contractProduct) {
        // 设置货物id
        contractProduct.setId(UUID.randomUUID().toString().replace("-", ""));
        // 总价格
        Double total = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(total);
        // 保存货物
        contractProductDao.insertSelective(contractProduct);
        // 根据货物id获取货物合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        System.out.println(contract);
        // 修改合同金额
        contract.setTotalAmount(contract.getTotalAmount() + total);
        // 修改货物数量
        contract.setProNum(contract.getProNum() + 1);
        // 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        // 1 根据货物的id获取以前的货物对象
        ContractProduct old_contractProduct = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        // 2 获取以前货物的总金额
        Double old_total = old_contractProduct.getAmount();
        //3 设置计算最新的总金额赋值给货物
        Double new_total = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(new_total);
        //4 修改货物
        contractProductDao.updateByPrimaryKeySelective(contractProduct);
        //5 查询合同根据货物的合同id
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //6 修改合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() - old_total + new_total);
        //7 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void delete(String id) {
        // 1 根据货物id获取货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        // 2 获取货物的总金额
        Double total=contractProduct.getAmount();
        //3 根据货物的id获取附件(list集合)
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria extCproductExampleCriteria = extCproductExample.createCriteria();
        //设置条件 根据货物的id查询附件
        extCproductExampleCriteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> list = extCproductDao.selectByExample(extCproductExample);
        //4 对附件进行遍历循环累加货物+附件的总金额
        if(list.size()>0){
            for (ExtCproduct extCproduct : list) {
                total += extCproduct.getAmount();
                //5 删除附件
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }
        //6 删除货物
        contractProductDao.deleteByPrimaryKey(id);
        //7 获取合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //8 设置合同的总金额
        contract.setTotalAmount(contract.getTotalAmount() - total);
        //9 设置合同的货物数和附件数
        contract.setProNum(contract.getProNum() - 1);
        contract.setExtNum(contract.getExtNum() - list.size());
        //10 修改合同
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        return contractProduct;
    }

    @Override
    public PageInfo findAll(ContractProductExample example, int page, int size) {
        PageHelper.startPage(page, size);
        List<ContractProduct> contractProducts = contractProductDao.selectByExample(example);
        PageInfo pageInfo = new PageInfo(contractProducts);
        return pageInfo;
    }

    @Override
    public void saveProductExcel(List<ContractProduct> list) {
        // 遍历循环list 获取每一个货物保存
        for (ContractProduct contractProduct : list) {
            // 调用以前保存的业务逻辑
            save(contractProduct);
        }
    }
}
